package cn.linshu.shiro.jwt.controller;

import cn.linshu.shiro.jwt.dto.CommonResult;
import cn.linshu.shiro.jwt.entity.SysUserEntity;
import cn.linshu.shiro.jwt.form.SysLoginForm;
import cn.linshu.shiro.jwt.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * -*- coding: utf-8 -*-
 *
 * @Author linnin
 * @Name springboot3-labs-master
 * @Time 2025/2/12 00:51
 * @Note
 * @For
 * @Out Copyright© 2025 linnin
 */

/* coding: utf-8 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    public CommonResult login(@RequestBody SysLoginForm form) throws Exception {
        SysUserEntity sysUserEntity = sysUserService.queryByUserName(form.getUsername());
        log.info("正确密码:{}", sysUserEntity.getPassword());
        log.info("当前密码:{}", new Sha256Hash(form.getPassword(), sysUserEntity.getSalt()).toHex());
        if (sysUserEntity == null || !sysUserEntity.getPassword().equals(new Sha256Hash(form.getPassword(), sysUserEntity.getSalt()).toHex())) {
            return CommonResult.failed(500, "账号或密码错误");
        }
        return CommonResult.success("login success");
    }
}
